package com.du.cloud.security.service;

import com.du.cloud.common.base.constant.RedisKeyConstants;
import lombok.SneakyThrows;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;

import javax.sql.DataSource;

/**
 * Desc:
 * Author: dushuang
 * Date: 2020/5/25
 **/
public class CustomClientDetailsService extends JdbcClientDetailsService {

    public CustomClientDetailsService(DataSource dataSource) {
        super(dataSource);
    }

    /**
     * 重写原生方法
     * 扩展：支持redis缓存 @Cacheable
     * @param clientId
     * @return
     * @throws InvalidClientException
     */
    @Override
    @SneakyThrows
    @Cacheable(value = RedisKeyConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null")
    public ClientDetails loadClientByClientId(String clientId){
        return super.loadClientByClientId(clientId);
    }
}
